package com.train.mgr.config.cron;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.train.mgr.config.redis.RedisLock;
import com.train.mgr.modules.biz.service.*;
import com.train.mgr.modules.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.UUID;

@Slf4j
@Component
public class ProjectCron {

    @Autowired
    private UserService userService;
    @Autowired
    private IStatCourseOverviewService statCourseOverviewService;
    @Autowired
    private IStatLessonTraineeService statLessonTraineeService;
    @Autowired
    private IStatLessonGroupService statLessonGroupService;
    @Autowired
    private IStatCourseGroupService statCourseGroupService;
    @Autowired
    private IStatCourseTraineeService statCourseTraineeService;
    @Autowired
    private RedisLock redisLock;


    @Async
    @Scheduled(cron = "0 0/10 * * * ?")
    public void executeUser() {
        log.info("用户授权期限状态任务时间{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        String key = "distLock:executeUser";
        String val = UUID.randomUUID().toString();
        boolean locked = redisLock.tryLock(key, val, 60_000); // 60s过期
        if (!locked) {
            log.info("【executeUser】未抢到锁，放弃执行");
            return;
        }
        try {
            int endedCount = userService.updateUserEnable();
            log.info("用户授权期限过期状态【开启】->【关闭】，更新数量:{}",endedCount);
        } finally {
            redisLock.unlock(key, val);
        }
    }

    @Async
    @Scheduled(cron = "0 0/5 * * * ?")
    public void courseOverview() {
        log.info("课程/课时学习总览统计{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        String key = "distLock:courseOverview";
        String val = UUID.randomUUID().toString();
        boolean locked = redisLock.tryLock(key, val, 60_000); // 60s过期
        if (!locked) {
            log.info("【courseOverview】未抢到锁，放弃执行");
            return;
        }
        try {
            statCourseOverviewService.buildLessonTraineeStat(null);
            log.info("课程/课时学习总览统计【开启】->【关闭】");
        } finally {
            redisLock.unlock(key, val);
        }
    }

    @Async
    @Scheduled(cron = "0 0/6 * * * ?")
    public void buildLessonTraineeStat() {
        log.info("课时学员学习统计{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        String key = "distLock:buildLessonTraineeStat";
        String val = UUID.randomUUID().toString();
        boolean locked = redisLock.tryLock(key, val, 60_000); // 60s过期
        if (!locked) {
            log.info("【buildLessonTraineeStat】未抢到锁，放弃执行");
            return;
        }
        try {
            statLessonTraineeService.buildLessonTraineeStat(null);
            log.info("课时学员学习统计【开启】->【关闭】");
        } finally {
            redisLock.unlock(key, val);
        }
    }

    @Async
    @Scheduled(cron = "0 0/7 * * * ?")
    public void buildLessonGroupStat() {
        log.info("课时分组学习统计{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        String key = "distLock:buildLessonGroupStat";
        String val = UUID.randomUUID().toString();
        boolean locked = redisLock.tryLock(key, val, 60_000); // 60s过期
        if (!locked) {
            log.info("【buildLessonGroupStat】未抢到锁，放弃执行");
            return;
        }
        try {
            statLessonGroupService.buildLessonTraineeStat(null);
            log.info("课时分组学习统计【开启】->【关闭】");
        } finally {
            redisLock.unlock(key, val);
        }
    }

    @Async
    @Scheduled(cron = "0 0/8 * * * ?")
    public void buildCourseTraineeStat() {
        log.info("课程学员学习统计{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        String key = "distLock:buildCourseTraineeStat";
        String val = UUID.randomUUID().toString();
        boolean locked = redisLock.tryLock(key, val, 60_000); // 60s过期
        if (!locked) {
            log.info("【buildCourseTraineeStat】未抢到锁，放弃执行");
            return;
        }
        try {
            statCourseTraineeService.buildLessonTraineeStat(null);
            log.info("课程学员学习统计【开启】->【关闭】");
        } finally {
            redisLock.unlock(key, val);
        }
    }

    @Async
    @Scheduled(cron = "0 0/9 * * * ?")
    public void buildCourseGroupStat() {
        log.info("课程分组学习统计{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        String key = "distLock:buildCourseGroupStat";
        String val = UUID.randomUUID().toString();
        boolean locked = redisLock.tryLock(key, val, 60_000); // 60s过期
        if (!locked) {
            log.info("【buildCourseGroupStat】未抢到锁，放弃执行");
            return;
        }
        try {
            statCourseGroupService.buildLessonTraineeStat(null);
            log.info("课程分组学习统计【开启】->【关闭】");
        } finally {
            redisLock.unlock(key, val);
        }
    }

//    @Async
//    @Scheduled(cron = "0 0/3 * * * ?")
//    public void updateFinishState() {
//        log.info("更新课时、课程完成状态{} 定时任务开启 =====================", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
//        statCourseGroupService.buildLessonTraineeStat();
//        log.info("更新课时、课程完成状态【开启】->【关闭】");
//    }
}
